<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>一朵 - 收货地址</title>
    <!-- swiper-2.7css -->
    <link rel="stylesheet" href="__STATIC__/index/js/swiper/idangerous.swiper.css">
    <!-- swiper-2.7js -->
    <script src="__STATIC__/index/js/swiper/idangerous.swiper.min.js"></script>
    <!-- jquery-3.2.1引用 -->
    <script src="__STATIC__/index/js/global/jquery-3.2.1.min.js"></script>
    <!-- 阿里巴巴矢量图库 -->
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_561814_6llb3s27vv64unmi.css">
    <!-- 公共头部 脚部 -->
    <link rel="stylesheet" href="__STATIC__/index/css/global/global.css">
    <!-- 超过1200屏幕 -->
    <link rel="stylesheet" media="screen and (min-width: 1200px)" href="__STATIC__/index/css/address/address1200.css" />
    <!-- 1080-1200 -->
    <link rel="stylesheet" media="screen  and (min-width: 1080px) and (max-width: 1199px) " href="__STATIC__/index/css/address/address1080.css"
    />
    <!-- 960-1080 -->
    <link rel="stylesheet" media="screen  and (min-width: 960px) and (max-width: 1079px) " href="__STATIC__/index/css/address/address960.css"
    />
    <!-- 768-959 -->
    <link rel="stylesheet" media="screen  and (min-width: 768px) and (max-width: 959px) " href="__STATIC__/index/css/address/address768.css"
    />
    <!-- 568-767 -->
    <link rel="stylesheet" media="screen  and (min-width: 568px) and (max-width: 767px) " href="__STATIC__/index/css/address/address568.css"
    />
    <!-- 375-567 -->
    <link rel="stylesheet" media="screen  and (min-width: 375px) and (max-width: 567px) " href="__STATIC__/index/css/address/address375.css"
    />
    <!-- 小于375-->
    <link rel="stylesheet" media="screen  and (max-width: 374px) " href="__STATIC__/index/css/address/address320.css" />
    <!--一引入layer-->
    <script src="__STATIC__/layer/layer.js"></script>
</head>
<body>
    <div class="M">
       <div class="m-header">
            <div>
                <span onclick="go_back();">
                    <span class="iconfont icon-xiangzuo" ></span>
                    返回
                </span>
                <span>收货地址</span>
                <span>
                    {if condition="!empty($realname)"}
                        <a href="{:url('User/index')}" class="my" >
                            {$realname}
                        </a>
                    {else /}
                        <a href="{:url('Login/loginin')}">登录</a>
                        <a href="{:url('Login/register')}">注册</a>
                    {/if}
                    <a href="{:url('MyCart/my_cart')}" class="car">
                        <span class="iconfont icon-gouwuche"></span>
                        <span>{$cart_num}</span>
                    </a>
                </span>
            </div>
        </div>
       {if condition="!empty($address)"}
        {foreach name="address" item="v"}
            <a href="{:url('Address/m_add_edit')}?id={$v.id}" class="m-address-xuanze ">
                <div>
                    <span>
                        <i class="iconfont icon-fangzi"></i>
                    </span>
                    <span>
                        <p>{$v.address}</p>
                        <p>
                            <span>{$v.consignee}</span>
                            <span>{$v.mobile}</span>
                        </p>
                    </span>
                    <span>
                        <i class="iconfont icon-iconset0416"></i>
                    </span>
                </div>
            </a>
        {/foreach}
        {/if}
        {if condition="empty($address)"}
            <div class="m-kong">
                <img src="__STATIC__/index/img/my/my.png" alt="">
                <p>这里什么都没有</p>
                <p>······</p>
            </div>
        {/if}
        <a href="{:url('Address/m_add_edit')}" class="m-bottom">
            <i class="iconfont icon-tianjia"></i>
            <span>
                添加收货地址
            </span>
        </a>
    </div>
    <div class="PC">
        {include file="public/p_header" /}
        <div class="pc-content">
            {include file="public/p_user" /}
            <div class="PC_right">
                {include file="public/p_user_info" /}
                <div class="pc-tihuan">
                    <div class="pc-tihuan-headline">
                        <div>
                            <span>
                                收货地址
                            </span>
                            <span>
                                <span class="pc-address-xin">
                                    添加收货地址
                                </span>
                            </span>
                        </div>
                    </div>
                    {if condition="!empty($address)"}
                        <div class="pc-bill">
                            {foreach name="address" item="v"}
                            <!-- 循环开始 -->
                            <div>
                                <span class="pc-name">{$v.consignee}</span>
                                <span class="pc-phone">{$v.mobile}</span>
                                <span>
                                    <span class="pc-address">{$v.city}</span>
                                    <span class="p_address">{$v.address}</span>
                                </span>
                                <span>
                                    <span>
                                        {if condition="$v.is_default eq 1"}
                                            <span class="pc-moren">[默认地址]</span>
                                        {else /}
                                            <a href="javascript:;" class="pc-shezhi" onclick="set_default({$v.id})">设置默认</a>
                                        {/if}
                                    </span>
                                    <i class="iconfont icon-pingjia " onclick="pc_compile(this,{$v.id},{$v.is_default})"></i>
                                    <i class="iconfont icon-lajitong" onclick="del_address({$v.id});"></i>
                                </span>
                            </div>
                            <!-- 循环结束 -->
                            {/foreach}
                        </div>
                    {/if}

                    {if condition="empty($address)"}
                    <div class="pc-kong">
                        <img src="__STATIC__/index/img/my/my.png" alt="">
                        <p>这里什么都没有</p>
                        <p>······</p>
                    </div>
                    {/if}
                </div>
            </div>
        </div>
        <!--添加-->
        <div class="pc-address-popups">
            <div class="pc-address-popups-content">
                <p>收货地址
                    <i class="iconfont icon-cuowu pc-address-esc"></i>
                </p>
                <input type="text" placeholder="请填写收货人" name="consignee" id="p_consignee">
                <div >
                    <input type="text" placeholder="请填写联系方式" name="mobile" id="p_mobile">
                </div>
                <select name="city" id="p_city">
                    {foreach name="city" item="v"}
                        <option value="{$v.value}">{$v.value}</option>
                    {/foreach}
                </select>
                <input type="text" placeholder="请填写详细地址" name="address" id="p_address">
                <label for="p_is_default">
                    <input type="checkbox" name="is_default" id="p_is_default" onclick="check_default(this)">
                    <span>设为默认地址</span>
                </label>
                <a href="javascript:;" onclick="check_form(1)">保存</a>
            </div>
        </div>
        <!--编辑-->
        <div class="pc-address-popups1">
            <div class="pc-address-popups1-content">
                <p>收货地址
                    <i class="iconfont icon-cuowu pc-address-esc1"></i>
                </p>
                <input type="text" placeholder="请填写收货人" name="consignee" id="p_e_consignee">
                <div>
                    <input type="text" placeholder="请填写联系方式" name="mobile" id="p_e_mobile">
                    <!--<span>格式错误</span>-->
                </div>
                <select name="p_city" id="p_e_city">
                    {foreach name="city" item="v"}
                        <option value="{$v.value}">{$v.value}</option>
                    {/foreach}
                </select>
                <input type="text" placeholder="请填写详细地址" name="address" id="p_e_address">
                <p>
                    <input type="checkbox" name="is_default" id="p_e_is_default" onclick="check_default(this)">
                    <span>设为默认地址</span>
                </p>
                <a href="javascript:;" onclick="check_e_form(1)">保存</a>
                <input type="hidden" name="address_id" >
            </div>
        </div>
        <!-- 公共脚部  -->
        {include file="public/p_footer" /}
        <!-- 公共脚部 结束 -->
    </div>
    <script>
        /**
         * 返回上一页
         */
        function go_back(){
            window.history.back();  //返回上一页
        }

        /**
         * 完善会员信息
         */
        function edit_member_info(){
            window.location.href = "{:url('User/my_center')}";
        }

        // pc端
        $('.pc-address-xin').click(function () {
            $('body').css('overflow', 'hidden');
            $('.pc-address-popups').css('display', 'block');
            $('.pc-address-popups-content').animate({
                bottom: '0',
            });
        });
        $('.pc-address-esc').click(function () {
            $('.pc-address-popups').css('display', 'none');
            $('body').css('overflow', 'scroll');
            $('.pc-address-popups-content').animate({
                bottom: '-400',
            });
        });
        function pc_compile(obj,id,is_default) {
            var consignee = $(obj).parent().parent().find('.pc-name').text();
            var mobile = $(obj).parent().parent().find('.pc-phone').text();
            var city = $(obj).parent().parent().find('.pc-address').text();
            var address = $(obj).parent().parent().find('.p_address').text();
            $('input[name=address_id]').val(id);
            console.log(address);
            $('#p_e_consignee').val(consignee);
            $('#p_e_mobile').val(mobile);
            $('#p_e_address').val(address);
            if(is_default == 1){
                $('#p_e_is_default').attr('checked',true);
            }
            $('#p_e_city').val(city);


            $('body').css('overflow', 'hidden');
            $('.pc-address-popups1').css('display', 'block');
            $('.pc-address-popups1-content').animate({
                bottom: '0',
            });

        };
        $('.pc-address-esc1').click(function () {
            $('.pc-address-popups1').css('display', 'none');
            $('body').css('overflow', 'scroll');
            $('.pc-address-popups1-content').animate({
                bottom: '-400',
            });
        });
        /**
         * 提交信息
         * type  1PC  2M
         */
        function check_form(type){
            var reg = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
            if(type == 1){
                var consignee = $('#p_consignee').val();
                var mobile = $('#p_mobile').val();
                var city = $('#p_city option:selected').val();
                var address = $('#p_address').val();
                if($('#p_is_default').hasClass('ischecked')){
                    var is_default = 1;
                }else{
                    var is_default = 0;
                }
            }

            if(consignee == ''){
                layer.msg('请输入收货人',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

            if(mobile == ''){
                layer.msg('请输入手机号',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

            if (!reg.test(mobile)) {
                layer.msg('请输入正确的手机号',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

            if(city == ''){
                layer.msg('请选择所在城市',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

            if(address == ''){
                layer.msg('请输入收货地址',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

            $.post("{:url('Address/add_edit')}",
                {consignee:consignee,mobile:mobile,city:city,address:address,is_default:is_default},
                function(data){
                    if(data.code == 1){
                        layer.msg('保存成功',{icon:1,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                            location.reload();
                        });
                        return false;
                    }else{
                        layer.msg(data.msg,{icon:2,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                        });
                        return false;
                    }
                }
            );
        }
        /**
         * 设置默认地址
         */
        function set_default(id){
            $.post("{:url('Address/set_default')}",
                {id:id},
                function(data){
                    if(data.code == 1){
                        layer.msg('设置成功',{icon:1,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                            location.reload();
                        });
                        return false;
                    }else{
                        layer.msg(data.msg,{icon:2,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                        });
                        return false;
                    }
                }
            );
        }

        function check_e_form(type){
            if(type == 1){
                var consignee = $('#p_e_consignee').val();
                var mobile = $('#p_e_mobile').val();
                var city = $('#p_e_city option:selected').val();
                var address = $('#p_e_address').val();
                var id = $('input[name=address_id]').val();
                if($('#p_e_is_default').hasClass('ischecked')){
                    var is_default = 1;
                }else{
                    var is_default = 0;
                }
            }

            $.post("{:url('Address/add_edit')}",
                {consignee:consignee,mobile:mobile,city:city,address:address,is_default:is_default,id:id},
                function(data){
                    if(data.code == 1){
                        layer.msg('保存成功',{icon:1,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                            location.reload();
                        });
                        return false;
                    }else{
                        layer.msg(data.msg,{icon:2,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                        });
                        return false;
                    }
                }
            );
        }

        /**
         * 选中默认
         * @param obj
         */
        function check_default(obj){
            if($(obj).hasClass('ischecked')){
                $(obj).removeClass('ischecked');
            }else{
                $(obj).addClass('ischecked');
            }
        }
        /**
         * 删除收货地址
         */
        function del_address(id){
            $.post("{:url('Address/del_address')}",
                {id:id},
                function(data){
                    if(data.code == 1){
                        layer.msg('删除成功',{icon:1,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                            location.reload();
                        });
                        return false;
                    }else{
                        layer.msg(data.msg,{icon:2,time:1500,shade: 0.1}, function(index){
                            layer.close(index);
                        });
                        return false;
                    }
                }
            );
        }

    </script>
</body>
</html>